<?
	include_once "./facebookQuery.php";
	
	
class Crawler
{
	
	private $posts;
	private $postsClean;
	private $PostsDeleted;
	public function __construct(Facebook $facebook, $MrX)
	{
		//Take all the posts by Facebook Query
		$fbQuery = new FacebookQuery($facebook);
		$this -> posts = $fbQuery -> allPosts($MrX);
		$this -> postsClean = array();
		$this -> PostsDeleted = array();
	}
	
	
	public function cleanPosts()
	{
		for($i = 0 ; $i < count($this -> posts ); $i++)
		{
			$delete = 0;
			//Extract only one post
			$post = $this -> posts[$i];
			
			//if there aren't Message or Story delete post
			if(!$post->message && !$post->story)
				$delete = 1;
				
			//if is a post or comment that Mr.X published but in an other wall delete 	
			if($this -> postInOtherWall($post))
				$delete = 1;
			
			//If there are tags, remove these one by the message
			if(count($post -> tags) > 0)
				$post -> message = $this -> removeTags($post);
			
			//If there are tags in the comment, remove these one by the comment--TO MAKE
			$post -> comments = $this -> deleteTagsComments($post);
			
			
			//Check if this is a special post like are now friend, her cousin etc etc....
			$specialPost = $this->specialPost($post);	
			
			
			//If there is a story and there isn't at least one comment or like we delete this message
			if($post -> story && !$post-> message && !$post -> numComments && !$post -> numLikes && 					               $specialPost[0])
			{
				if($specialPost[0] == 1)
					array_push($this -> PostsDeleted, $post->id); 
				//echo "<br/> DELETE POST ID: ".$post -> id."<br/>";
				$delete = 1;
			}
			
			if($specialPost[0])
			{
				if($specialPost[0] > 1 )
					$post -> application = 'facebook';
				$post -> type = $specialPost[1];
			}
			
			
			if($delete == 0)
				array_push($this->postsClean , $post);

				
		}
					
		echo "<br> POSTS DELETED:  ".count($this -> PostsDeleted)."<br>";
		return $this-> postsClean;
		
	}
	
	private function removeTags($post)
	{

		foreach($post -> tags as $tag)
		{
			$post -> message = ereg_replace($tag['name'],'',$post->message);

		}

		
		return $post -> message;
	}
	
	private function deleteTagsComments($post)
	{
		for($i = 0 ; $i < count($post -> comments); $i++)
		{
			$comment = $post -> comments[$i];
			if($comment['message_tags'])
			{
				foreach($comment['message_tags'] as $tag)
					$comment['message'] = ereg_replace($tag['name'],'',$comment['message']);
			
				$post -> comments[$i] = $comment;	
			}
			
		}
		
		return $post -> comments;
	}
	
	private function postInOtherWall($post)
	{
		if(substr_count($post -> story,'on his own post on') == 1 ||
		   substr_count($post -> story,'on her own post on') == 1 ||
		   substr_count($post -> story,'was tagged in') == 1)
   	 		   return 1;
		 
		  $pattern = '/.*(on){1}.*(photo.){1}/'; 					 
		  if(preg_match($pattern,$post->story))
		  	   return 1;

  		  $pattern = '/.*(on){1}.*(link.){1}/';
		  if(preg_match($pattern,$post->story))
		       return 1;

   		  $pattern = '/.*(on){1}.*(status.){1}/';
		  if(preg_match($pattern,$post->story))
		       return 1;

   		  $pattern = '/.*(on){1}.*(Wall.){1}/';
	      if(preg_match($pattern,$post->story))
		 	   return 1;		  
		
   		  $pattern = '/.*(on){1}.*(wall.){1}/';
	      if(preg_match($pattern,$post->story))
		 	   return 1;
			      		
		  $pattern = '/.*(on){1}.*(post.){1}/';
	      if(preg_match($pattern,$post->story))
		 	   return 1;
		  
		  $pattern = '/.*(on){1}.*(video.){1}/';
	      if(preg_match($pattern,$post->story))
		 	   return 1;
			   
		  $pattern = '/.*(on){1}.*(note.){1}/';
	      if(preg_match($pattern,$post->story))
		 	   return 1;			   
		  
		  $pattern = '/.*(posted a){1}.*(Wall.){1}/';
	      if(preg_match($pattern,$post->story))
		 	   return 1;		  	
			   	
		return 0;	   
	}
	
	
		
	private function specialPost($post)
	{
		
		
		
		if(substr_count($post -> story,'shared') == 1)
		{
			$result = array(1,'share');
			return $result;
		}

		if(substr_count($post -> story,'added a new photo') == 1)
		{
			$result = array(1,'photo');
			return $result;
		}
		
		if(substr_count($post -> story,'recommends') == 1)
		{
			$result = array(2,'recommends');
			return $result;
		}
		
		if(substr_count($post -> story,'added') == 1)
		{
			$result = array(2,'added');
			return $result;
		}
		
		if(substr_count($post -> story,'changed') == 1)
		{
			$result = array(2,'changed');
			return $result;
		}
		
		if(substr_count($post -> story,'started playing') == 1)
		{
			$result = array(2,'playing');
			return $result;
		}
		
	
		if(substr_count($post -> story,'joined the group') == 1)
		{
			$result = array(2,'join');
			return $result;
		}
		
		
		if(substr_count($post -> story,'went to') == 1)
		{
			$result = array(2,'went to');
			return $result;
		}
		
		if(substr_count($post -> story,'started using') == 1)
		{
			$result = array(2,'using');
			return $result;
		}

		if(substr_count($post -> story,'likes') == 1)
		{
			$result = array(2,'likes page');
			return $result;
		}
		
		if(substr_count($post -> story,'updated') == 1)
		{
			$result = array(2,'updated');
			return $result;
		}

		if(substr_count($post -> story,'are now friends') == 1 ||
			substr_count($post -> story,'is now friends') == 1)
		{
			$result = array(2,'friend');
			return $result;
		}
		if(substr_count($post -> story,'listed') == 1)
		{
			$result = array(2,'family');
			return $result;
		}
		
		if(substr_count($post -> story,'is in a relationship with') == 1 ||
		   substr_count($post -> story,'went from being') == 1 ||
		   substr_count($post -> story,'is married') == 1 ||
		   substr_count($post -> story,'is engaged') == 1 ||
		   substr_count($post -> story,'is widower') == 1 ||
		   substr_count($post -> story,'is widow') == 1 ||
		   substr_count($post -> story,'divorced') == 1 ||	
		   substr_count($post -> story,'complicated relationship') == 1 ||
		   substr_count($post -> story,'single') == 1 ||
   		   substr_count($post -> story,'separed') == 1 )
		   {
			   	$result = array(2,'relationship');
				return $result;
		   }
		   
		
		return array(0,'');
	}

}

?>